package com.zxx.casserver.modules.dictionary.service.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.google.common.base.Joiner;
import com.zxx.cascommon.base.exception.DataNoChangeException;
import com.zxx.cascommon.base.exception.DataNotExistException;
import com.zxx.casserver.modules.dictionary.entity.DictionaryDO;
import com.zxx.casserver.modules.dictionary.entity.DictionaryVO;
import com.zxx.casserver.modules.dictionary.mapper.DictionaryMapper;
import com.zxx.casserver.modules.dictionary.service.IDictionaryService;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

/**
 * 字典服务-具体逻辑处理层
 *
 * @author zhoukai
 * @date 2020/5/8 16:27
 */
@Service
public class DictionaryServiceImpl extends ServiceImpl<DictionaryMapper, DictionaryDO> implements
        IDictionaryService {

    @Override
    public IPage<DictionaryDO> search(DictionaryVO dictionaryVO) {
        Page<DictionaryDO> page = new Page<>(dictionaryVO.getCurPage(), dictionaryVO.getPageSize());
        QueryWrapper<DictionaryDO> queryWrapper = new QueryWrapper<>();
        if (!ObjectUtils.isEmpty(dictionaryVO.getIds())) {
            queryWrapper.in("dict_id", dictionaryVO.getIds());
        }
        if (!ObjectUtils.isEmpty(dictionaryVO.getDictId())) {
            queryWrapper.eq("dict_id", dictionaryVO.getDictId());
        }
        if (!ObjectUtils.isEmpty(dictionaryVO.getAppId())) {
            queryWrapper.eq("app_id", dictionaryVO.getAppId());
        }
        if (!ObjectUtils.isEmpty(dictionaryVO.getLabel())) {
            queryWrapper.eq("label", dictionaryVO.getLabel());
        }
        if (!ObjectUtils.isEmpty(dictionaryVO.getValue())) {
            queryWrapper.eq("value", dictionaryVO.getValue());
        }
        if (!ObjectUtils.isEmpty(dictionaryVO.getType())) {
            queryWrapper.eq("type", dictionaryVO.getType());
        }
        if (!ObjectUtils.isEmpty(dictionaryVO.getGroup())) {
            queryWrapper.eq("group", dictionaryVO.getGroup());
        }
        if (!ObjectUtils.isEmpty(dictionaryVO.getStartTime())) {
            queryWrapper.ge("create_time", dictionaryVO.getStartTime());
        }
        if (!ObjectUtils.isEmpty(dictionaryVO.getEndTime())) {
            queryWrapper.le("create_time", dictionaryVO.getEndTime());
        }
        if (!ObjectUtils.isEmpty(dictionaryVO.getColumns())
                && !ObjectUtils.isEmpty(dictionaryVO.getAsc())) {
            String columnNames = Joiner.on(",").join(dictionaryVO.getColumns());
            if (dictionaryVO.getAsc()) {
                queryWrapper.orderByAsc(columnNames);
            } else {
                queryWrapper.orderByDesc(columnNames);
            }
        } else {
            queryWrapper.orderByDesc("create_time");
        }
        return page(page, queryWrapper);
    }

    @Override
    public DictionaryDO saveDictionary(DictionaryVO dictionaryVO) {
        //根据type+label判断是否已经存在
        DictionaryDO findDictionary = findDictionaryByTypeAndLabel(dictionaryVO.getGroup(), dictionaryVO.getLabel());
        if (!ObjectUtils.isEmpty(findDictionary)) {
            throw new DataNotExistException();
        }
        DictionaryDO dictionary = JSON.parseObject(JSON.toJSONString(dictionaryVO), DictionaryDO.class);
        dictionary.setCreateTime(System.currentTimeMillis());
        dictionary.setUpdateTime(System.currentTimeMillis());

        boolean success = save(dictionary);
        if (success) {
            return dictionary;
        }
        return null;
    }

    /**
     * 根据type+label查询字典项信息
     *
     * @param group
     * @param label
     * @author zhoukai
     * @date 2020/5/8 17:31
     */
    private DictionaryDO findDictionaryByTypeAndLabel(String group, String label) {
        QueryWrapper<DictionaryDO> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("group", group);
        queryWrapper.eq("label", label);
        return getOne(queryWrapper);
    }

    @Override
    public DictionaryDO updateDictionary(DictionaryVO dictionaryVO) {
        //根据type+label判断是否已经存在
        DictionaryDO findDictionary = findDictionaryByTypeAndLabel(dictionaryVO.getGroup(), dictionaryVO.getLabel());
        if (ObjectUtils.isEmpty(findDictionary)) {
            throw new DataNotExistException();
        }
        DictionaryDO dictionary = judgeChange(dictionaryVO, findDictionary);
        if (ObjectUtils.isEmpty(dictionary)) {
            throw new DataNoChangeException();
        }
        boolean success = updateById(dictionary);
        if (success) {
            return dictionary;
        }
        return null;
    }

    @Override
    public boolean batchDelete(DictionaryVO dictionaryVO) {
        return removeByIds(dictionaryVO.getIds());
    }

    /**
     * 判断是否有变化的值
     *
     * @param dictionaryVO   传入的值
     * @param findDictionary 数据库值
     * @author zhoukai
     * @date 2020/5/8 18:29
     */
    private DictionaryDO judgeChange(DictionaryVO dictionaryVO, DictionaryDO findDictionary) {
        DictionaryDO dictionaryDO = new DictionaryDO();
        dictionaryDO.setDictId(dictionaryVO.getDictId());
        dictionaryDO.setUpdateTime(System.currentTimeMillis());
        dictionaryDO.setAppId(dictionaryVO.getAppId());
        boolean change = false;
        if (!ObjectUtils.isEmpty(dictionaryVO.getGroup()) &&
                !dictionaryVO.getGroup().equals(findDictionary.getGroup())) {
            dictionaryDO.setGroup(dictionaryVO.getGroup());
            change = true;
        }
        if (!ObjectUtils.isEmpty(dictionaryVO.getType()) &&
                !dictionaryVO.getType().equals(findDictionary.getType())) {
            dictionaryDO.setType(dictionaryVO.getType());
            change = true;
        }
        if (!ObjectUtils.isEmpty(dictionaryVO.getLabel()) &&
                !dictionaryVO.getLabel().equals(findDictionary.getLabel())) {
            dictionaryDO.setLabel(dictionaryVO.getLabel());
            change = true;
        }
        if (!ObjectUtils.isEmpty(dictionaryVO.getValue()) &&
                !dictionaryVO.getValue().equals(findDictionary.getValue())) {
            dictionaryDO.setValue(dictionaryVO.getValue());
            change = true;
        }
        return change ? dictionaryDO : null;
    }
}
